package com.fingard.avatar.website.config;

import com.fingard.avatar.website.service.UserAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

/**
 * @author 徐一涛
 * @date 2022/4/26
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    protected UserAuthService authService;
    @Autowired
    protected GlobalProperties globalProperties;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //注册TestInterceptor拦截器
        InterceptorRegistration registration = registry.addInterceptor(new AuthenticationInterceptor(authService));
        //所有路径都被拦截
        registration.addPathPatterns("/**");
        //添加不拦截路径 登录路径 html静态资源 js静态资源 css静态资源
        registration.excludePathPatterns(
                "/public/*",
                "/**/*.html",
                "/**/*.js",
                "/**/*.css",
                "/img/*"
        );
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/img/**").addResourceLocations(String.format("file:%s", globalProperties.getFilePath()));
    }
}
